<template>
  <el-card class="box-card">
    <el-form :inline="true" :model="form">
      <el-form-item label="手机号">
        <el-input v-model="form.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="短信渠道">
        <el-select v-model="form.channelId" placeholder="请选择短信渠道">
          <el-option
            v-for="item in channelList"
            :key="item.id"
            :value="item.id"
            :label="item.signature"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="模板编号">
        <el-input v-model="form.templateId" placeholder="请输入模板编号" />
      </el-form-item>
      <el-form-item label="发送状态" prop="sendStatus">
        <el-select v-model="form.sendStatus" placeholder="请选择发送状态" clearable>
          <el-option label="全部" value="" />
          <el-option
            v-for="item in sendStatusList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="接收状态">
        <el-select v-model="form.receiveStatus" placeholder="请选择接收状态" clearable>
          <el-option label="全部" value="" />
          <el-option
            v-for="item in receiveStatusList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" style="margin-top: 10px">
        <el-date-picker
          v-model="date1"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <!-- <el-form-item label="接收时间" style="margin-top: 10px">
        <el-date-picker
          v-model="date2"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item> -->
      <el-form-item style="float: right; margin-top: 10px">
        <el-button type="primary" @click="search">搜索</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格部分 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      ></el-table-column>
      <el-table-column label="手机号" align="center" prop="mobile" width="120"></el-table-column>
      <el-table-column label="短信内容" align="center" prop="templateContent" width="250" />
      <el-table-column label="发送状态" align="center" width="180">
        <template v-slot="scope">
          <el-tag :type="sendStatusComputed(scope.row.sendStatus).type">
            {{ sendStatusComputed(scope.row.sendStatus).label }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="接收状态" align="center" width="180">
        <template v-slot="scope">
          <el-tag :type="receiveStatusComputed(scope.row.receiveStatus).type">
            {{ receiveStatusComputed(scope.row.receiveStatus).label }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="短信渠道" align="center" width="120">
        <template v-slot="scope">
          <div>{{ channelComputed(scope.row.channelId) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="模板编号" align="center" prop="templateId" />
      <el-table-column label="短信类型" align="center" prop="templateType">
        <template v-slot="scope">
          <div>{{ typeComputed(scope.row.templateType) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template v-slot="scope">
          <el-button
            type="info"
            size="mini"
            v-if="$formatFR(log.detail)"
            plain
            @click="handledetail(scope.row)"
          >
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :size.sync="form.size"
      :current.sync="form.current"
      :total="total"
      @pagination="getList"
    />
    <DetailDialog
      ref="DetailDialog"
      @updatePage="reset"
      :typeList="typeList"
      :channelList="channelList"
      :sendStatusList="sendStatusList"
      :receiveStatusList="receiveStatusList"
    />
  </el-card>
</template>

<script>
import { page } from "@/api/message/sms/log"
import { simplePage } from "@/api/message/sms/channel"
import DetailDialog from "./components/DetailDialog"
import FUNCROLELIST from "@/utils/func_role/message"
export default {
  components: {
    DetailDialog
  },
  data() {
    return {
      typeList: [
        {
          label: "验证码",
          value: "1"
        },
        {
          label: "通知",
          value: "2"
        },
        {
          label: "营销",
          value: "3"
        }
      ],
      log: FUNCROLELIST.sms.log,
      form: {
        size: 10,
        current: 1
      },
      date1: [],
      date2: [],
      total: 0,
      tableData: [],
      channelList: [],
      sendStatusList: [
        {
          label: "初始化",
          value: 0,
          type: "info"
        },
        {
          label: "发送成功",
          value: 10,
          type: "primary"
        },
        {
          label: "发送失败",
          value: 20,
          type: "danger"
        },
        {
          label: "忽略",
          value: 30,
          type: "info"
        }
      ],
      receiveStatusList: [
        {
          label: "初始化",
          value: 0,
          type: "primary"
        }
      ]
    }
  },
  mounted() {
    this.getList()
    this.simplePage()
  },
  computed: {
    typeComputed() {
      return function (v) {
        return this.typeList.find((item) => item.value == v)?.label
      }
    },
    channelComputed() {
      return function (v) {
        return this.channelList.find((item) => item.id == v)?.signature
      }
    },
    sendStatusComputed() {
      return function (v) {
        const info = this.sendStatusList.find((item) => item.value === v)
        return info
          ? info
          : {
              type: "info",
              label: ""
            }
      }
    },
    receiveStatusComputed() {
      return function (v) {
        const info = this.receiveStatusList.find((item) => item.value === v)
        return info
          ? info
          : {
              type: "info",
              label: ""
            }
      }
    }
  },
  methods: {
    reset() {
      this.date1 = []
      this.date2 = []
      this.form = {
        size: 10,
        current: 1
      }
      this.getList()
    },
    search() {
      this.form.current = 1
      this.getList()
    },

    handledetail(scope) {
      this.$refs.DetailDialog.init(scope)
    },
    async getList() {
      if (this.date1.length) {
        this.form.createStartTime = this.date1[0]
        this.form.createEndTime = this.date1[1]
      }
      // if (this.date2.length) {
      //   this.form.receiveStartTime = this.date2[0]
      //   this.form.receiveEndTime = this.date2[1]
      // }
      const { data } = await page(this.form)
      this.tableData = data.records
      this.total = data.total
    },
    simplePage() {
      simplePage().then((res) => {
        this.channelList = res.data
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dropdown-item-p {
  display: inline;
}
.dropdown {
  margin-left: 10px;
}
</style>
